<?php

namespace app\logic\single;

use app\api\model\order\OrderMusicApiModel;
use app\api\model\order\OrderMusicDetailApiModel;
use app\api\model\user\UserWriterWaterApiModel;
use app\api\model\UserModel;
use app\api\model\vip\OrderVipApiModel;
use app\api\model\vip\VipApiModel;
use think\facade\Db;

class OrderVipSingle
{

    /**
     * @var OrderVipSingle
     */
    private static $inc;
    /**
     * @var OrderVipApiModel
     */
    private $orderModel;
    /**
     * @var VipApiModel
     */
    private $vipModel;
    /**
     * @var UserModel
     */
    private $userModel;

    private function __clone(){

    }
    // 禁止被实例化
    private function __construct()
    {
        $this->vipModel = new VipApiModel();
        $this->orderModel = new OrderVipApiModel();
        $this->userModel = new UserModel();
    }
    public static function getInstance(){
        if(is_null(self::$inc)){
            self::$inc = new self();
        }
        return self::$inc;
    }
    public function complete(string $order_id='', int $pay_type = 10, array $other = []){
        $order = $this->orderModel->where('id',$order_id)->find();
        if(empty($order)){
            $order = $this->orderModel->where('order_no',$order_id)->find();
        }
        if(empty($order)){
            return [-1,'订单不存在'];
        }
        if($order['status'] == 20){//如果已完成，则不能支付
            return [-1,'订单不能完成'];
        }
        $vip = $this->vipModel->where('id',$order['vip_id'])->find();
        $user = $this->userModel->getFindById($order['buyer_id']);
        if(empty($vip)){
            return [-1,'vip不存在'];
        }
        if(empty($user)){
            return [-1,'用户不存在'];
        }
        //判断用户vip过期时间
        if($vip['expire_time'] == -1){
            $vip_end_time = '2099-12-31 23:59:59';
        }
        else{
            //判断给予时间
            if($user['vip_id'] ==-1){//不是VIP，直接累加时间
                $vip_end_time = date('Y-m-d 23:59:59',time()+ $vip['expire_time'] * 86400);
            }else{//是VIP
                $user_vip = $this->vipModel->where('id',$user['vip_id'])->find();
                if(!empty($user_vip)){
                    if($user_vip['level'] > $vip['level']){
                        return [-1,'当前已是更高等级vip'];
                    }
                }
                if($user['vip_id'] != $vip['id']){//用户当前vip和购买vip不是同一个
                    $vip_end_time = date('Y-m-d 23:59:59',time()+ $vip['expire_time'] * 86400);
                }else{//用户当前vip和购买vip是同一个
                    if($user['vip_end_time'] < date('Y-m-d H:i:s')){//过期了
                        $vip_end_time = date('Y-m-d 23:59:59',time()+ $vip['expire_time'] * 86400);
                    }else{//累加
                        $now_exp_time = strtotime($user['vip_end_time']);
                        $end_time = $vip['expire_time'] * 86400;
                        $vip_end_time = date('Y-m-d 23:59:59',$now_exp_time+ $end_time);
                    }
                }
            }
        }
        Db::startTrans();
        try {
            $order->pay_type = $pay_type;
            $order->status = 20;
            $order->pay_time = date('Y-m-d H:i:s');
            if(isset($other['trade_no'])){
                $order->trade_no = $other['trade_no'];
            }
            $order->save();
            UserModel::where('id',$order['buyer_id'])->save([
                'vip_id'=>$vip['id'],
                'vip_end_time'=>$vip_end_time,
            ]);
            //给予积分
            if($vip['give_score']>0){
                UserScoreSingle::getInstance()->createLog($order['buyer_id'],1,$vip['give_score'],2,0,'购买会员');
            }
            Db::commit();
            return true;
        }catch (\Exception $exception){
            Db::rollback();
            return [-1,$exception->getMessage()];
            return false;
        }
    }
}